package 多线程.线程池;

import org.junit.Test;

import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Description
 *
 * @Author: Heiffeng
 * @Date: 2022/1/3 17:23
 */
public class 使用SynchronousQueue做队列的线程池使用测试 {


    private final static ThreadPoolExecutor pool = new ThreadPoolExecutor(
            2,
            10,
            30L,
            TimeUnit.SECONDS,
            new SynchronousQueue<>(),
            (r, executor) ->{
                try {
                    executor.getQueue().put(r);
                } catch (InterruptedException e) {e.printStackTrace();}
            }
    );

    @Test
    public void 阻塞提交测试(){
        while(true){
            pool.execute(()->{
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {}
                System.out.println(Thread.currentThread().getName() + "is running ...");
            });
        }
    }
}
